package com.five.admin.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.five.admin.mapper.MenoMapper;
import com.five.admin.server.MenuService;
import com.five.admin.vo.MenuMeta;
import com.five.admin.vo.MenuRouterVo;
import com.five.admin.vo.MenuTreeVo;
import com.five.pojo.entity.Menu;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends ServiceImpl<MenoMapper, Menu> implements MenuService {

    @Override
    public List<MenuTreeVo> getMenuTree() {
        //查询所有menu
        List<Menu> menus = getBaseMapper().selectList(null);
        return buildTree(menus);
    }

    @Override
    public List<MenuRouterVo> getMenuRouter() {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("type", 2);
        List<Menu> menus = getBaseMapper().selectList(queryWrapper);
        return menus.stream().filter(menu -> menu.getParentId() == 0).map(menu -> {
            return MenuRouterVo.builder()
                    .name(menu.getName())
                    .path(menu.getPath())
                    .component(menu.getComponent())
                    .redirect(menu.getRedirect())
                    .sort(menu.getSort())
                    .meta(MenuMeta.builder().title(menu.getName()).icon(menu.getIcon()).build())
                    .children(menus.stream().filter(item -> item.getParentId().equals(menu.getId())).map(item -> {
                        return MenuRouterVo.builder()
                                .name(item.getName())
                                .path(item.getPath())
                                .component(item.getComponent())
                                .redirect(item.getRedirect())
                                .meta(MenuMeta.builder().title(item.getName()).icon(item.getIcon()).build())
                                .sort(item.getSort())
                                .build();
                    }).collect(Collectors.toList()))
                    .build();
        }).collect(Collectors.toList());
    }

    @Override
    public List<Menu> getRoleHasMenu(Long id) {
//        List<Menu> roleHasMenu = getBaseMapper().getRoleHasMenu(id);
        return getBaseMapper().getRoleHasMenu(id);
    }

    @Override
    public void deleteRoleMenu(Long menuID) {
        getBaseMapper().deleteRoleAndMenu(menuID);
    }

    /**
     * 构建树形结构
     * @param menus
     * @return
     */
    private List<MenuTreeVo> buildTree(List<Menu> menus) {
        //形成父子关系
        //获取根节点
        List<Menu> root = menus.stream().filter(menu -> menu.getParentId() == 0).collect(Collectors.toList());
        return root.stream().map(menu -> {
            List<Menu> rootChildren = menus.stream().filter(item -> item.getParentId().equals(menu.getId())).collect(Collectors.toList());
            List<MenuTreeVo> list = rootChildren.stream().map(item -> {
                MenuTreeVo treeRootChildren = new MenuTreeVo();
                treeRootChildren.setId(item.getId());
                treeRootChildren.setIcon(item.getIcon());
                treeRootChildren.setPath(item.getPath());
                treeRootChildren.setComponent(item.getComponent());
                treeRootChildren.setType(item.getType());
                treeRootChildren.setParentId(menu.getId());
                treeRootChildren.setSort(menu.getSort());
                treeRootChildren.setParentName(menu.getName());

                treeRootChildren.setName(item.getName());
                List<Menu> children = menus.stream().filter(child -> child.getParentId().equals(item.getId())).collect(Collectors.toList());
                //转化
                List<MenuTreeVo> treeChildren = children.stream().map(child -> {
                    MenuTreeVo menuTreeVo = new MenuTreeVo();
                    menuTreeVo.setId(child.getId());
                    menuTreeVo.setName(child.getName());
                    menuTreeVo.setIcon(child.getIcon());
                    menuTreeVo.setPath(child.getPath());
                    menuTreeVo.setComponent(child.getComponent());
                    menuTreeVo.setType(child.getType());
                    menuTreeVo.setParentId(item.getId());
                    menuTreeVo.setSort(child.getSort());
                    menuTreeVo.setParentName(item.getName());
                    return menuTreeVo;
                }).collect(Collectors.toList());
                //设置子节点
                treeRootChildren.setChildren(treeChildren);
                return treeRootChildren;
            }).collect(Collectors.toList());
            return MenuTreeVo.builder()
                    .id(menu.getId())
                    .name(menu.getName())
                    .icon(menu.getIcon())
                    .type(menu.getType())
                    .path(menu.getPath())
                    .component(menu.getComponent())
                    .children(list)
                    .sort(menu.getSort())
                    .build();
        }).collect(Collectors.toList());
    }


}
